package cn.dqy.gotanserver.code;

import cn.dqy.gotanserver.dto.GoTanInet;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GoTanDtoEncode extends MessageToByteEncoder<GoTanInet> {
    static Logger logger = LoggerFactory.getLogger(GoTanDtoEncode.class);

    //规则 {[报文长度][文本长度][文本][图片长度][图片]}
    @Override
    protected void encode(ChannelHandlerContext ctx, GoTanInet msg, ByteBuf out) throws Exception {
        logger.info("开始编码,GoTanInet={}",msg);
        out.writeInt(msg.getPic_data().length+msg.getVal_data().length + 8); //整个数据包长度
        out.writeInt(msg.getVal_data().length);
        out.writeBytes(msg.getVal_data());
        out.writeInt(msg.getPic_data().length);
        out.writeBytes(msg.getPic_data());
        logger.info("编码完毕");
    }
}
